package com.bjc.netty.c3;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @program: nettypro
 * @description: netty future
 * @author: bjc
 * @create: 2021-09-23 21:54
 */
@Slf4j
public class TestNettyFuture {

    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();
        Future<Integer> future = eventLoop.submit(() -> {
            log.debug("执行计算");
            TimeUnit.SECONDS.sleep(1L);
            return 50;
        });
        //同步
        /*log.debug("等待结果");
        log.debug("结果是：{}",future.get());*/

        // 异步方式获取结果
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.debug("接受结果：{}",future.getNow());
            }
        });
    }
}
